package com.epro.dx.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.weipu.common.util.Logger;

import static com.epro.dx.service.ScreenManager.BROAD_FINISH_ACTIVITY;

/**
 * 一个单像素的Activity，由于是单像素，用户不可见
 * @author Zenglw
 * @version 1.0
 * @date 2017/7/7
 * @see [相关类]
 */
public class LiveActivity extends AppCompatActivity {

    private static final String TAG = "LiveActivity";
    private FinishBroadCast receiver;

    class FinishBroadCast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if (BROAD_FINISH_ACTIVITY.equals(intent.getAction())) {
                if (!isFinishing()) {
                    finish();
                }
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.e(TAG, "onCreate: " );
        registerBroadcast();

        //创建一个单像素的Activity，此时界面是全黑的，需要给这个Activity设置主题
        Window window = getWindow();
        window.setGravity(Gravity.START | Gravity.TOP);
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 0;
        params.y = 0;
        params.width = 1;
        params.height = 1;
        window.setAttributes(params);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        Logger.e(TAG,"onNewIntent");
        super.onNewIntent(intent);
    }

    public void registerBroadcast(){

        IntentFilter filter = new IntentFilter(BROAD_FINISH_ACTIVITY);
        receiver = new FinishBroadCast();
        this.registerReceiver(receiver,filter);
    }

    public void unRegisterbroadcast(){
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Logger.e(TAG, "onDestroy: ");
        unRegisterbroadcast();
    }
}
